package org.example.registry.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisConnectionManager {

    private static final String REDIS_HOST = "82.156.183.185";
    private static final int REDIS_PORT = 6379;
    private static JedisPool jedisPool;

    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(100); // 最大连接数
        poolConfig.setMaxIdle(20); // 最大空闲连接数
        poolConfig.setMinIdle(5); // 最小空闲连接数
        poolConfig.setMaxWaitMillis(1000 * 10); // 最长等待时间

        jedisPool = new JedisPool(poolConfig, REDIS_HOST, REDIS_PORT);
    }

    public static Jedis getResource() {
        return jedisPool.getResource();
    }

    public static void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedisPool.returnResource(jedis);
//            jedisPool.returnResourceObject(jedis);
        }
    }
}